package com.tansun.easycare.rule.executionblock.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tansun.easycare.rule.codeblock.service.RuleCodeblockService;
import com.tansun.easycare.rule.common.Constant;
import com.tansun.easycare.rule.common.service.RuleCommService;
import com.tansun.easycare.rule.common.service.impl.RuleBaseServiceImpl;
import com.tansun.easycare.rule.datamodel.dao.RuleDataparamsDao;
import com.tansun.easycare.rule.datamodel.dao.RuleDatasetDao;
import com.tansun.easycare.rule.datamodel.entity.RuleDataparams;
import com.tansun.easycare.rule.exception.BizException;
import com.tansun.easycare.rule.executionblock.dao.RuleExcutionBlockDao;
import com.tansun.easycare.rule.executionblock.entity.RuleExcutionBlock;
import com.tansun.easycare.rule.executionblock.model.ExecutionBlockVO;
import com.tansun.easycare.rule.executionblock.service.ExecutionBlockService;
import com.tansun.easycare.rule.referencedata.entity.RuleReferenceData;
import com.tansun.easycare.rule.referencedata.service.RuleReferenceDataService;
import com.tansun.easycare.rule.ruleflow.entity.RuleFlowConditions;
import com.tansun.easycare.rule.ruleflow.model.RuleFlowItem;
import com.tansun.easycare.rule.rulemange.entity.RuleManage;
import com.tansun.easycare.rule.rulemange.service.RuleManageService;
import com.tansun.easycare.rule.ruleversion.entity.RuleVersioninfo;
import com.tansun.easycare.rule.ruleversion.service.RuleVersioninfoService;
import com.tansun.rule.common.RuleModelTypeEnum;
import com.tansun.rule.utils.StringUtils;

@Service("executionBlockService")
public class ExecutionBlockServiceImpl extends RuleBaseServiceImpl<RuleExcutionBlockDao, RuleExcutionBlock> implements ExecutionBlockService {

    @Autowired
    private RuleManageService ruleManageService;

    @Autowired
    private RuleVersioninfoService ruleVersioninfoService;
	@Autowired
	private  RuleReferenceDataService  ruleReferenceDataService;
    @Autowired
    private RuleDataparamsDao ruleDataparamsDao;


    public ExecutionBlockVO findExecutionBlock(String id, String projectCode, String version) throws BizException {
        ExecutionBlockVO executionBlockVO = new ExecutionBlockVO();
        RuleManage ruleManage = this.ruleManageService.get(id);
        if (ruleManage == null) throw new BizException("E-000600");
        executionBlockVO.setRuleManage(ruleManage);
        RuleExcutionBlock executionBlockInfo = new RuleExcutionBlock();
        executionBlockInfo.setManageId(ruleManage.getId());
        if (StringUtils.isEmpty(version))
            version = ruleManage.getVersion();
        executionBlockInfo.setBlockVersion(version);
        List<RuleExcutionBlock> exList = this.findList(executionBlockInfo);
//        for (RuleExcutionBlock excutionBlock : exList) {
//        	//判断，如果modelType=40是函数变量，另一个方法获取参数
//        	if("40".equals(excutionBlock.getModelType())) {
//        		excutionBlock.setExeCodes(this.ruleCommService.findSelectModelFunctionParamList(projectCode, excutionBlock.getModelType()));
//        	}else {
//				excutionBlock.setExeCodes(this.ruleCommService.findSelectModelList(projectCode, excutionBlock.getModelType()));
//				excutionBlock.setVersions(this.ruleCommService.findSelectPublishVersionList(projectCode, excutionBlock.getExecCode()));
//        	}
//        }
        executionBlockVO.setExcutionBlocks(exList);

        return executionBlockVO;
    }


    @Override
    public List<RuleVersioninfo> findRuleRefVersion(String projectCode, String code, String version) throws BizException {
        RuleManage ruleManageInfo = new RuleManage();
        ruleManageInfo.setCode(code);
        ruleManageInfo.setProjectCode(projectCode);
        ruleManageInfo.setVersion(version);
        List<RuleManage> list = this.ruleManageService.findList(ruleManageInfo);
        List<RuleVersioninfo> results = new ArrayList();
        if (list != null && list.size() > 0) {
            RuleExcutionBlock excutionBlock = new RuleExcutionBlock();
            RuleManage ruleManage = list.get(0);
            excutionBlock.setManageId(ruleManage.getId());
            //excutionBlock.setBlockVersion(ruleManage.getVersion());
            excutionBlock.setBlockVersion(version);
            excutionBlock.setProjectCode(projectCode);
            excutionBlock.setVersion(version);
            List<RuleExcutionBlock> eList = this.findList(excutionBlock);
            RuleVersioninfo versioninfo = null;
            for (RuleExcutionBlock blockInfo : eList) {
                versioninfo = new RuleVersioninfo();
                // versioninfo.setVersion(blockInfo.getVersion());
                versioninfo.setModelCode(blockInfo.getExecCode());
                versioninfo.setProjectCode(projectCode);
                versioninfo.setIsValid("1");
                List<RuleVersioninfo> resultList = this.ruleVersioninfoService.findNewPublishVersionList(versioninfo);
                if(resultList.size()>0) {
	                results.add(resultList.get(0));
                }
            }
            versioninfo = new RuleVersioninfo();
            versioninfo.setManageId(ruleManage.getId());
            versioninfo.setVersion(version);
            //versioninfo.setVersion(ruleManage.getVersion());
            versioninfo.setModelCode(ruleManage.getCode());
            results.addAll(this.ruleVersioninfoService.findPublishVersionList(versioninfo));
        }
        return results;

    }
    
    
    public Map<String,String> findRuleRefModelVersion(String projectCode, String code, String version) throws BizException {
        RuleManage ruleManageInfo = new RuleManage();
        ruleManageInfo.setCode(code);
        ruleManageInfo.setProjectCode(projectCode);
        ruleManageInfo.setVersion(version);
        List<RuleManage> list = this.ruleManageService.findList(ruleManageInfo);
        Map<String,String>  resultMap  = new  HashMap<>();
        if (list != null && list.size() > 0) {
            RuleExcutionBlock excutionBlock = new RuleExcutionBlock();
            RuleManage ruleManage = list.get(0);
            excutionBlock.setManageId(ruleManage.getId());
            excutionBlock.setBlockVersion(ruleManage.getVersion());
            List<RuleExcutionBlock> eList = this.findList(excutionBlock);
            RuleVersioninfo versioninfo = null;
            for (RuleExcutionBlock blockInfo : eList) {
            	if (RuleModelTypeEnum.DecisionTree.getCode().equals(blockInfo.getModelType())) {
            	   Map<String,String>  varMap  = new  HashMap<String,String>();
              	   varMap.put("code", blockInfo.getExecCode());
            	   varMap.put("projectCode", projectCode);
            	   List<String> codesList = this.ruleVersioninfoService.findRuleRefeCodes(varMap);
                   if (codesList ==null || codesList.size() <1) {
                	   continue;
                   }
                   Map<String,Object>  vrMap = new  HashMap<String,Object>();
                   vrMap.put("projectCode", projectCode);
                   vrMap.put("refeList", codesList);
                   List<RuleVersioninfo> verList=this.ruleVersioninfoService.findRuleRefeVersion(vrMap);
                   for (RuleVersioninfo  verInfo:verList) {
                	   resultMap.put(verInfo.getModelCode(), verInfo.getRuleContent());
                   }
            	}

            }
        }
        return resultMap;

    }

    

    @Override
    public void saveOrUpdate(ExecutionBlockVO executionBlockVO) throws BizException {
        RuleManage ruleManage = executionBlockVO.getRuleManage();
        ruleManage.setModelType(RuleModelTypeEnum.ExecutionBlock.getCode());
        ruleManage.setStatus(Constant.RULE_STATUS_NORMAL);
        this.ruleManageService.saveOrUpdate(ruleManage);
        List<RuleExcutionBlock> list = executionBlockVO.getExcutionBlocks();
        super.insertBatch(list, new String[]{"manageId", "blockVersion"},
                new Object[]{ruleManage.getId(), ruleManage.getVersion()});
        //收集引用数据
        Map<String, RuleReferenceData> refMap = new HashMap<>();
        RuleVersioninfo ruleVersioninfo = new RuleVersioninfo();
        ruleVersioninfo.init(ruleManage, this.buildDataBpmnContent(executionBlockVO,ruleManage,refMap));
        //ruleVersioninfo.setIsValid("0");
        this.ruleVersioninfoService.save(ruleVersioninfo);
        //保存引用数据
        this.ruleReferenceDataService.saveReferenceData(refMap);
    }

    private String buildDataBpmnContent(ExecutionBlockVO model,RuleManage ruleManage,Map<String, RuleReferenceData> refMap) throws BizException {
    	   String space = "    ";
           String wrap = "\n";
          // String codeId = model.getRuleManage().getCode() + "_" + model.getRuleManage().getVersion();
           String codeId = model.getRuleManage().getCode() ;
           List<RuleExcutionBlock> excutionBlocks = model.getExcutionBlocks();

           StringBuffer content = new StringBuffer();
           content.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + wrap);
           content.append("<definitions id=\"Definition\"" + wrap);
           content.append(space + "targetNamespace=\"http://www.jboss.org/drools\"" + wrap);
           content.append(space + "typeLanguage=\"http://www.java.com/javaTypes\"" + wrap);
           content.append(space + "expressionLanguage=\"http://www.mvel.org/2.0\"" + wrap);
           content.append(space + "xmlns=\"http://www.omg.org/spec/BPMN/20100524/MODEL\"" + wrap);
           content.append(space + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" + wrap);
           content.append(space + "xsi:schemaLocation=\"http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd\"" + wrap);
           content.append(space + "xmlns:g=\"http://www.jboss.org/drools/flow/gpd\"" + wrap);
           content.append(space + "xmlns:bpmndi=\"http://www.omg.org/spec/BPMN/20100524/DI\"" + wrap);
           content.append(space + "xmlns:dc=\"http://www.omg.org/spec/DD/20100524/DC\"" + wrap);
           content.append(space + "xmlns:di=\"http://www.omg.org/spec/DD/20100524/DI\"" + wrap);
           content.append(space + "xmlns:tns=\"http://www.jboss.org/drools\">" + wrap);
           content.append(wrap);

           content.append(space + "<process processType=\"Private\" isExecutable=\"true\" id=\"" + codeId + "\" name=\"\" tns:packageName=\"com.tansun.ruleflow\" tns:version=\"\">" + wrap);

           content.append(space + space + "<extensionElements>" + wrap);
           content.append(space + space + space + "<tns:import name=\"java.util.*\"/>" + wrap);
           content.append(space + space + space + "<tns:import name=\"com.tansun.easycare.rule.utils.*\" />" + wrap);
           content.append(space + space + space + "<tns:global identifier=\"ruleHelper\" type=\"com.tansun.rule.utils.RuleHelper\" />" + wrap);
           content.append(space + space + space + "<tns:global identifier=\"droolsRuleLog\" type=\"com.tansun.rule.utils.DroolsRuleLog\" />" + wrap);
           content.append(space + space + space + "<tns:global identifier=\"dataMap\" type=\"java.util.Map\" />" + wrap);
           content.append(space + space + space + "<tns:global identifier=\"metaMap\" type=\"java.util.Map\" />" + wrap);

           content.append(space + space + "</extensionElements>" + wrap);
           content.append(wrap);
           content.append(space + space + "<laneSet>" + wrap);
           content.append(space + space + space + "<lane name=\"swimName01\"></lane>" + wrap);
           content.append(space + space + space + "<lane name=\"swimName02\"></lane>" + wrap);
           content.append(space + space + "</laneSet>" + wrap);
           content.append(wrap);
           content.append(space + space + "<!-- nodes -->" + wrap);
           content.append(space + space + "<startEvent id=\"rect1\"/>" + wrap);
           int  len  =  excutionBlocks.size();
           if (len >1)
              content.append(space + space + "<inclusiveGateway id=\"rect_fork"  + "\" name=\"exec_fork"   + "\" gatewayDirection=\"Diverging\"/>" + wrap);
           for (int i = 0; i < excutionBlocks.size(); i++) {
               RuleExcutionBlock node = excutionBlocks.get(i);
               String id = "rect" + (i + 3);
               //content.append(space + space + "<businessRuleTask id=\"" + id + "\" name=\"" + node.getExecName() + "\" g:ruleFlowGroup=\"" + node.getExecCode() + "_" + node.getVersion() + "\">" + wrap);
               content.append(space + space + "<businessRuleTask id=\"" + id + "\" name=\"" + node.getExecName() + "\" g:ruleFlowGroup=\"" + node.getExecCode()  + "\">" + wrap);
               content.append(space + space + space + "<ioSpecification>" + wrap);
               content.append(space + space + space + space + "<inputSet>" + wrap);
               content.append(space + space + space + space + "</inputSet>" + wrap);
               content.append(space + space + space + space + "<outputSet>" + wrap);
               content.append(space + space + space + space + "</outputSet>" + wrap);
               content.append(space + space + space + "</ioSpecification>" + wrap);
               content.append(space + space + "</businessRuleTask>" + wrap);
           }
           if (len >1)
             content.append(space + space + "<exclusiveGateway id=\"rect_join"  + "\" name=\"exec_join" + "\" gatewayDirection=\"Converging\"/>" + wrap);
           content.append(space + space + "<endEvent id=\"rect2\" name=\"\"/>" + wrap);
           content.append(wrap);
           content.append(space + space + "<!-- connections -->" + wrap);
           String lineId = "rect1-rect_fork";
           String from = "rect1";
           String to = "rect_fork";
           String mapStr = "ruleHelper.trueFlag==true";


           if (len ==1) {
               lineId = "rect1-rect3";
               from = "rect1";
               to = "rect3";
	           content.append(space + space + "<sequenceFlow id=\"" + lineId + "\" sourceRef=\"" + from + "\" targetRef=\"" + to + "\" />" + wrap);
               lineId = "rect3-rect2";
               from = "rect3";
               to = "rect2";
	           content.append(space + space + "<sequenceFlow id=\"" + lineId + "\" sourceRef=\"" + from + "\" targetRef=\"" + to + "\" />" + wrap);
        	   
           }else if(len >1) {
               lineId = "rect1-rect_fork";
               from = "rect1";
               to = "rect_fork";
	           content.append(space + space + "<sequenceFlow id=\"" + lineId + "\" sourceRef=\"" + from + "\" targetRef=\"" + to + "\" />" + wrap);
	           
	           for (int i = 0; i < excutionBlocks.size() ; i++) {
	               from = "rect_fork" ;
	               to = "rect" + (i + 3);
	               lineId = "rect_fork" + "-rect" +(i+3) ;
	               content.append(space + space + "<sequenceFlow id=\"" + lineId + "\" sourceRef=\"" + from + "\" targetRef=\"" + to + "\">" +wrap);
	               content.append(space + space + space + "<conditionExpression xsi:type=\"tFormalExpression\" >" + mapStr + "</conditionExpression>" + wrap);
	               content.append(space + space + "</sequenceFlow>" + wrap);
	           }
	           for (int i = 0; i < excutionBlocks.size() ; i++) {
	               from = "rect" + (i + 3);
	               to = "rect_join" ;
	               lineId =  "rect" +(i+3)+"rect_join"  ;
	               content.append(space + space + "<sequenceFlow id=\"" + lineId + "\" sourceRef=\"" + from + "\" targetRef=\"" + to + "\"/>"+wrap );
	           }
	           from = "rect_join" ;
	           to = "rect2";
	           lineId = from + "-" + to;
	           content.append(space + space + "<sequenceFlow id=\"" + lineId + "\" sourceRef=\"" + from + "\" targetRef=\"" + to + "\"/>" + wrap);

           }
           content.append(space + "</process>" + wrap);
           content.append(wrap);
           content.append(space + "<bpmndi:BPMNDiagram>" + wrap);
           content.append(space + space + "<bpmndi:BPMNPlane bpmnElement=\"" + codeId + "\">" + wrap);
           //bpmndi:BPMNShape
           int width = 50;
           int height = 50;
           String id = "rect1";
           int x = 130;
           int y = 150;
           content.append(space + space + space + "<bpmndi:BPMNShape bpmnElement=\"" + id + "\">" + wrap);
           content.append(space + space + space + space + "<dc:Bounds x=\"" + x + "\" y=\"" + y + "\" width=\"" + width + "\" height=\"" + height + "\"/>" + wrap);
           content.append(space + space + space + "</bpmndi:BPMNShape>" + wrap);
           for (int i = 0; i < excutionBlocks.size(); i++) {
               width = 110;
               height = 48;
               x = 130 + 200 * (i + 1);
               id = "rect" + (i + 3);
               content.append(space + space + space + "<bpmndi:BPMNShape bpmnElement=\"" + id + "\">" + wrap);
               content.append(space + space + space + space + "<dc:Bounds x=\"" + x + "\" y=\"" + y + "\" width=\"" + width + "\" height=\"" + height + "\"/>" + wrap);
               content.append(space + space + space + "</bpmndi:BPMNShape>" + wrap);
           }

           width = 50;
           height = 50;
           x = 130 + 200 * excutionBlocks.size() + 200;
           id = "rect2";
           content.append(space + space + space + "<bpmndi:BPMNShape bpmnElement=\"" + id + "\">" + wrap);
           content.append(space + space + space + space + "<dc:Bounds x=\"" + x + "\" y=\"" + y + "\" width=\"" + width + "\" height=\"" + height + "\"/>" + wrap);
           content.append(space + space + space + "</bpmndi:BPMNShape>" + wrap);

           //BPMNEdge
           content.append(wrap);

           for (int i = 0; i <= excutionBlocks.size(); i++) {
               if (i == 0) {
                   lineId = "rect1-rect3";
               } else if (i == excutionBlocks.size()) {
                   lineId = "rect" + (i + 2) + "-rect2";
               } else {
                   lineId = "rect" + (i + 2) + "-rect" + (i + 3);
               }
               content.append(space + space + space + "<bpmndi:BPMNEdge bpmnElement=\"" + lineId + "\">" + wrap);
               //from坐标
               if (i == 0) {
                   width = 50;
                   height = 50;
                   x = 130;
               } else {
                   width = 110;
                   height = 48;
                   x = 130 + 200 * i;

               }
               content.append(space + space + space + space + "<di:waypoint x=\"" + (x + width / 2) + "\" y=\"" + (y + height / 2) + "\"/>" + wrap);

               //to坐标
               if (i == excutionBlocks.size()) {
                   width = 50;
                   height = 50;
                   x = 130 + 200 * excutionBlocks.size() + 200;
               } else {
                   width = 110;
                   height = 48;
                   x = 130 + 200 * (i + 1);
               }
               content.append(space + space + space + space + "<di:waypoint x=\"" + (x + width / 2) + "\" y=\"" + (y + height / 2) + "\"/>" + wrap);
               content.append(space + space + space + "</bpmndi:BPMNEdge>" + wrap);
           }
           content.append(space + space + "</bpmndi:BPMNPlane>" + wrap);
           content.append(space + "</bpmndi:BPMNDiagram>" + wrap);
           content.append("</definitions>" + wrap);
           System.out.println(content.toString());
           return content.toString();


    }

    
    @Override
    public void updateVersion(RuleManage ruleManage) throws BizException {
        RuleExcutionBlock excutionBlock = new RuleExcutionBlock();
        if (ruleManage.getOldId() != null) {
            excutionBlock.setManageId(ruleManage.getOldId());
        } else {
            excutionBlock.setManageId(ruleManage.getId());
        }
        excutionBlock.setBlockVersion(ruleManage.getOldVersion());
        List<RuleExcutionBlock> list = this.findList(excutionBlock);
        if (list != null) {
            for (RuleExcutionBlock ruleExcutionBlock : list) {
                ruleExcutionBlock.setId(null);
                ruleExcutionBlock.setManageId(ruleManage.getId());
                ruleExcutionBlock.setBlockVersion(ruleManage.getVersion());
                this.save(ruleExcutionBlock);
            }
        }

    }


    @Override
    public void copyRule(RuleManage oldRuleManage, RuleManage ruleManage) throws BizException {
        ruleManage.setOldVersion(oldRuleManage.getVersion());
        ruleManage.setOldId(oldRuleManage.getId());
        this.updateVersion(ruleManage);

    }
    
    public String createCondition(String conditionJSON,RuleManage ruleManage,Map<String, RuleReferenceData> refMap) {
    	String mapStr = "";
    	if (StringUtils.isNotEmpty(conditionJSON)) {
            List<RuleFlowItem> conList = JSON.parseArray(conditionJSON, RuleFlowItem.class);
            String  enmStr=null;
            String key = "";
            for (RuleFlowItem forkItem : conList) {
                RuleFlowConditions condition = new RuleFlowConditions(forkItem);
                key = condition.getClassName() + "|" + condition.getNameen() + "|in";
                ruleReferenceDataService.collectCommonData(condition, key, "in", ruleManage, refMap);
                String dataType = forkItem.getDataType();
                String domain = forkItem.getDomain();
                String symbol = forkItem.getSymbol();
                String controlValue = forkItem.getControlValue();
                enmStr =null;
                if (dataType.toLowerCase().equals("string") || dataType.toLowerCase().equals("emun")) {
                    if (StringUtils.isNotEmpty(domain)) {
                        // 类型字符串、domain不为空、操作符为0:   not in
                    	controlValue = controlValue.substring(1, controlValue.length() - 1);
               
                    	if (controlValue != null) {
                    		controlValue=StringUtils.replace(controlValue, "\"", "");
                    	}
                        if (symbol.equals("0")) {
                            //symbol = "!=";
                        	enmStr = "ruleHelper.isNotContains(dataMap,\"" + forkItem.getClassName() + "." + forkItem.getNameEN() + "\""+",\""+controlValue+"\")";
                        }
                        // 类型字符串、domain不为空、操作符为1:   in
                        else if (symbol.equals("1")) {
                           // symbol = "contains";
                        	enmStr = "ruleHelper.isExistsEnum(dataMap,\"" + forkItem.getClassName() + "." + forkItem.getNameEN() + "\""+",\""+controlValue+"\")";
                        }
                        // 处理value值  0,1  --->  ("0", "1")
                       // controlValue = controlValue.substring(1, controlValue.length() - 1);
                        //controlValue = "(" + controlValue + ")";
                    } else {
                        if (symbol.equals("startsWith") || symbol.equals("endsWith")) {
                            symbol = "." + symbol;
                            controlValue = "(\"" + controlValue + "\")";
                        } else {
                            controlValue = "\"" + controlValue + "\"";
                        }
                    }
                } else if (dataType.toLowerCase().equals("double") || dataType.toLowerCase().equals("long") || dataType.toLowerCase().equals("date")) {
                    // 操作符进行转译
                    if (symbol.equals(">")) {
                        symbol = "&gt;";
                    } else if (symbol.equals(">=")) {
                        symbol = "&gt;=";
                    } else if (symbol.equals("<")) {
                        symbol = "&lt;";
                    } else if (symbol.equals("<=")) {
                        symbol = "&lt;=";
                    }
                    symbol = " " + symbol + " ";
                    if (dataType.toLowerCase().equals("date")) {
                        controlValue = "\"" + controlValue + "\"";
                    }else {
			            //20171103 modify 
                    	if (controlValue.startsWith("{") && controlValue.endsWith("}")) {
							controlValue=StringUtils.replace(controlValue, "￥", "\"");
							Map<String, String> controlMap;
							try {
								controlMap = (new ObjectMapper()).readValue(controlValue, Map.class);
								controlValue = genernalWhenStr(controlMap.get("className"),controlMap.get("nameEN"),"ruleflow"); 
								//controlValue = " ruleHelper.getValue(dataMap,\"" + controlMap.get("className") + "." + controlMap.get("nameEN") + "\")";
								key = controlMap.get("className") + "|" + controlMap.get("nameEN") + "|in";
								ruleReferenceDataService.collectFromMap(controlMap, key, "in", ruleManage, refMap);
							} catch (IOException e) {
								e.printStackTrace();
							}
						}
                    }
                } else if (dataType.toLowerCase().equals("boolean")) {
                    controlValue = "";
                    // 类型字符串、domain为空、操作符为0:   !=
                    if (symbol.equals("0")) {
                        controlValue = "false";
                    }
                    // 类型字符串、domain为空、操作符为1:   ==
                    else if (symbol.equals("1")) {
                        controlValue = "true";
                    }
                    symbol = " == ";
                }
                String leftOpt = forkItem.getLeftOpt();
                if (!StringUtils.isEmpty(leftOpt)) {
                    leftOpt = leftOpt.toUpperCase();
                    leftOpt = leftOpt.replace("AND", "&amp;&amp;").replace("OR", "||");
                }
                String rightOpt = forkItem.getRightOpt();
                if (!StringUtils.isEmpty(rightOpt)) {
                    rightOpt = rightOpt.toUpperCase();
                    rightOpt = rightOpt.replace("AND", "&amp;&amp;").replace("OR", "||");
                }
                leftOpt = leftOpt == null ? "" : leftOpt;
                rightOpt = rightOpt == null ? "" : rightOpt;
                if (enmStr != null) {
                	mapStr +=leftOpt+enmStr +rightOpt;
                }else {
                	RuleDataparams ruleDataparams = new RuleDataparams();
                	ruleDataparams.setProjectcode(ruleManage.getProjectCode());
                	ruleDataparams.setClassName(forkItem.getClassName());
                	ruleDataparams.setName(forkItem.getNameEN());
                	List<RuleDataparams> list = ruleDataparamsDao.findList(ruleDataparams);
                	String tmpStr = null;
                	if(list!=null && list.size()>0) {
                		ruleDataparams = list.get(0);
                		if("1".equals(ruleDataparams.getIsfuncparam())) {
                			tmpStr = forkItem.getClassName()+"_"+forkItem.getNameEN()+"(dataMap,metaMap)";
                		}else {
                			tmpStr = genernalWhenStr(forkItem.getClassName(),forkItem.getNameEN(),ruleManage.getModelType()); 
                		}
                	}else {
                		tmpStr = genernalWhenStr(forkItem.getClassName(),forkItem.getNameEN(),ruleManage.getModelType()); 
                	}
                	mapStr += leftOpt + tmpStr + symbol +" " + controlValue + rightOpt;
                }
            }
        }
    	return mapStr;
    }
    
 
     


}
